# Maintainer: Felix Yan <felixonmars@archlinux.org>

_hkgname=mysql-simple
pkgname=haskell-mysql-simple
pkgver=0.4.9
pkgrel=15
pkgdesc="A mid-level MySQL client library"
url="https://github.com/paul-rouse/mysql-simple"
license=("BSD")
arch=('loong64' 'x86_64')
depends=('ghc-libs' 'haskell-attoparsec' 'haskell-base16-bytestring' 'haskell-blaze-builder'
         'haskell-blaze-textual' 'haskell-mysql' 'haskell-old-locale' 'haskell-pcre-light')
makedepends=('ghc' 'haskell-hspec')
checkdepends=('mariadb')
source=("https://hackage.haskell.org/packages/archive/$_hkgname/$pkgver/$_hkgname-$pkgver.tar.gz")
sha512sums=('ccec9fbd2599b549a12237c46c6f992004853cffa21519dd7f7c6febdc83d7b7d6bf0edeb49c69691f2777b3e8bb855ac98c1c0ac299b27bab85124d8b83d189')

build() {
  cd $_hkgname-$pkgver

  runhaskell Setup configure -O --enable-shared --enable-executable-dynamic --disable-library-vanilla \
    --prefix=/usr --docdir=/usr/share/doc/$pkgname --datasubdir=$pkgname --enable-tests \
    --dynlibdir=/usr/lib --libsubdir=\$compiler/site-local/\$pkgid --ghc-option=-fllvm \
    --ghc-option=-optl-Wl\,-z\,relro\,-z\,now \
    --ghc-option='-pie'

  runhaskell Setup build $MAKEFLAGS
  runhaskell Setup register --gen-script
  runhaskell Setup unregister --gen-script
  sed -i -r -e "s|ghc-pkg.*update[^ ]* |&'--force' |" register.sh
  sed -i -r -e "s|ghc-pkg.*unregister[^ ]* |&'--force' |" unregister.sh
}

check() {
  cd $_hkgname-$pkgver
  mkdir -p /tmp/mysql_test
  mysql_install_db --datadir=/tmp/mysql_test
  mysqld --datadir=/tmp/mysql_test --socket=/tmp/socket.mysql --skip-grant-tables &
  MYSQL_PID=$!
  sleep 5
  mysql -h 127.0.0.1 -u root -e "create database IF NOT EXISTS test;"

  runhaskell Setup test --show-details=direct

  kill $MYSQL_PID
}

package() {
  cd $_hkgname-$pkgver

  install -D -m744 register.sh "$pkgdir"/usr/share/haskell/register/$pkgname.sh
  install -D -m744 unregister.sh "$pkgdir"/usr/share/haskell/unregister/$pkgname.sh
  runhaskell Setup copy --destdir="$pkgdir"
  install -D -m644 LICENSE -t "$pkgdir"/usr/share/licenses/$pkgname/
  rm -f "$pkgdir"/usr/share/doc/$pkgname/LICENSE
}
